package com.sicheng.lc.lc笔试.腾讯音乐;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/9/26 19:11
 */
public class 连续子数组的数量 {
    // 求有多少连续子数组的元素乘积 0的个数大于等于x
    // 防止结果太大对 1e9 + 7 取模
    // a.length <= 1e5,  1 <= a[i],x <= 1e9
    static int mod = (int) (1e9 + 7);
    public int getSubarrayNum(ArrayList<Integer> a, int x) {
        // write code here
        // 样例：
        // a =[5,2,3,50,4], x = 2 ===> 6
        // [5,2,3,50,4],[50,4],[3,50,4],[2,3,5,10,4],[2,3,50],[5,2,3,50]
        int[] sum2 = new int[a.size() + 1];
        int[] sum5 = new int[a.size() + 1];
        int res = 0;
        int c2 = 0;
        int c5 = 0;
        for (int i = 0; i < a.size(); i++) {
            int y = a.get(i);

            while (y % 2 == 0) {
                c2++;
                y /= 2;
            }
            while (y % 5 == 0) {
                c5++;
                y /= 5;
            }
            sum2[i + 1] = c2;
            sum5[i + 1] = c5;
            if (Math.min(c2, c5) < x)
                continue;
            int min = Math.min(find(sum2, c2 - x, i), find(sum5, c5 - x, i));
            res = (res + min + 1) % mod;
        }
        return res;
    }

    int find(int[] a, int x, int end) {
        int l = 1, r = end;
        while (l <= r) {
            int mid = l + r >> 1;
            if (a[mid] <= x) {
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        return r;
    }

    public static void main(String[] args) {
        连续子数组的数量 s = new 连续子数组的数量();
        Integer[] a = {5, 2, 3, 50, 4};
        List<Integer> list = Arrays.asList(a);
        int res = s.getSubarrayNum(new ArrayList<>(list), 2);
        System.out.println(res);
    }
}
